#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
时间计算器 - 一键打包脚本
使用方法: python 简单打包.py
"""

import os
import sys
import subprocess

def main():
    print("=" * 50)
    print("    时间计算器 - 简单打包工具")
    print("=" * 50)
    print()
    
    # 检查环境
    if not os.path.exists('src/main.py'):
        print("错误: 请在项目根目录运行此脚本")
        input("按回车键退出...")
        return
    
    # 安装PyInstaller
    print("检查PyInstaller...")
    try:
        import PyInstaller
        print("PyInstaller已安装")
    except ImportError:
        print("正在安装PyInstaller...")
        subprocess.run([sys.executable, "-m", "pip", "install", "PyInstaller"])
    
    # 直接使用命令行打包
    print("\n开始打包...")
    print("这可能需要几分钟时间...")
    
    cmd = [
        sys.executable, "-m", "PyInstaller",
        "--onefile",
        "--noconsole", 
        "--name=时间计算器",
        "--add-data=src/ui;ui",
        "--add-data=src/core;core", 
        "--add-data=src/utils;utils",
        "--hidden-import=PyQt5.QtCore",
        "--hidden-import=PyQt5.QtGui",
        "--hidden-import=PyQt5.QtWidgets",
        "--hidden-import=pendulum",
        "--hidden-import=chinese_calendar",
        "--hidden-import=zhdate",
        "--hidden-import=pandas",
        "--hidden-import=openpyxl",
        "--hidden-import=ui.main_window",
        "--hidden-import=ui.widgets.date_calculator_tab",
        "--hidden-import=ui.widgets.time_calculator_tab", 
        "--hidden-import=ui.widgets.special_functions_tab",
        "--hidden-import=ui.widgets.history_tab",
        "--hidden-import=core.date_calculator",
        "--hidden-import=core.time_calculator",
        "--hidden-import=core.holiday_data",
        "--hidden-import=core.lunar_calendar",
        "--hidden-import=utils.history_manager",
        "src/main.py"
    ]
    
    try:
        result = subprocess.run(cmd, check=True)
        print("\n打包成功!")
        
        # 移动exe文件
        import shutil
        if os.path.exists("dist/时间计算器.exe"):
            shutil.move("dist/时间计算器.exe", "时间计算器.exe")
            print("exe文件已生成: 时间计算器.exe")
            
            # 清理临时文件
            if os.path.exists("dist"):
                shutil.rmtree("dist")
            if os.path.exists("build"):
                shutil.rmtree("build")
            print("临时文件已清理")
        
    except subprocess.CalledProcessError as e:
        print(f"打包失败: {e}")
    
    input("\n按回车键退出...")

if __name__ == "__main__":
    main()
